package com.focus.pay.ali.action.order;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.ali.constant.Constant;
import com.focus.pay.ali.constant.AliChannelType;
import com.focus.pay.ali.model.cancel.AliPayModelCancel;
import com.focus.pay.ali.model.order.AliPayModelCreate;
import com.focus.pay.core.enums.Channel;
import com.focus.pay.ali.model.order.AliPayModelCodeScan;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;

/**
 * @Author: 赵参谋
 * @Date: 2021/9/28 15:15
 * @Description: 支付宝扫码付款-生成二维码链接
 * 生成二维码，用户扫描二维码支付
*/
@PayOrderHandler(channel = Channel.AliPay,channelType = AliChannelType.CodeScan)
public class AliPayOrderCodeScan extends AliPayOrder {

    /**
     * 统一收单线下交易预创建
     * @param aliPayModelCodeScan 下单参数
     * @param aliPayConfig 支付参数
     * @return 二维码链接
     */
    public AlipayTradePrecreateResponse payOrder(AliPayModelCodeScan aliPayModelCodeScan, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setNotifyUrl(aliPayModelCodeScan.getNotifyUrl());
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelCodeScan));
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            return response;
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 统一收单交易创建接口
     * @param aliPayModelCreate 创建交易接口信息
     * @param aliPayConfig 配置信息
     * @return 创建交易结果
     */
    public AlipayTradeCreateResponse payCreate(AliPayModelCreate aliPayModelCreate, AliPayConfig aliPayConfig) throws PayException{
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
            request.setNotifyUrl(aliPayModelCreate.getNotifyUrl());
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelCreate));
            return alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 统一收单交易撤销接口
     * @param aliPayModelCreate 交易撤销信息
     * @param aliPayConfig 配置信息
     * @return 创建交易结果
     */
    public AlipayTradeCancelResponse payCancel(AliPayModelCancel aliPayModelCreate, AliPayConfig aliPayConfig) throws PayException{
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeCancelRequest request = new AlipayTradeCancelRequest ();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelCreate));
            return alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }
}
